﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CypherKeys
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void globalEventProvider1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '`')
            { //Encode
                object tmp = Clipboard.GetDataObject();
                SendKeys.SendWait("^a");
                SendKeys.SendWait("^c");
                System.Threading.Thread.Sleep(50);
                Cleartext.Text = Clipboard.GetText();
                string data = "#" + RijndaelSimple.Encrypt(
                    Cleartext.Text,
                    passPhrase.Text,
                    Properties.Settings.Default.salt,
                    Properties.Settings.Default.algorithm,
                    Properties.Settings.Default.iterations,
                    Properties.Settings.Default.vector,
                    Properties.Settings.Default.keysize).Replace("+", "{+}");
                SendKeys.SendWait(data);
                Clipboard.SetDataObject(tmp);
                e.Handled = true;
            }
            else if (e.KeyChar == '~')
            { //Encode
                //get data from clipboard
                e.Handled = true;
                object tmp = Clipboard.GetDataObject();

                //get the hash
                SendKeys.SendWait("^c");
                System.Threading.Thread.Sleep(50);
                string crypt = "";
                if (Clipboard.ContainsText())
                    crypt = Clipboard.GetText();
                else
                {
                    MessageBox.Show("No crypto found. cempty\n" + crypt);
                    Clipboard.SetDataObject(tmp);
                    return;
                }
                //return previous data to clipboard
                Clipboard.SetDataObject(tmp);

                //parse
                //Text = crypt;
                if (crypt.IndexOf('#') > 0) 
                    crypt = crypt.Substring(crypt.IndexOf('#')+1);
                else if (crypt.StartsWith("#"))
                    crypt = crypt.Substring(1);
                else
                {
                    MessageBox.Show("No crypto found.\n" + crypt);
                    return;
                }

                //decode
                try
                {
                    Cleartext.Text = RijndaelSimple.Decrypt(
                        crypt,
                        passPhrase.Text,
                        Properties.Settings.Default.salt,
                        Properties.Settings.Default.algorithm,
                        Properties.Settings.Default.iterations,
                        Properties.Settings.Default.vector,
                        Properties.Settings.Default.keysize);
                }
                catch (FormatException ex)
                {
                    MessageBox.Show("error decoding\nError: "+ex.Message+"\n\nHash: " + crypt);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("nope:\n" + ex.Message + ex.StackTrace);
                }

                //show cleartext 
                this.Focus();

            }
        }
    }
}
